package com.pt25.base.dep.nacos.encrypt;

import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyResolver;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;

/**
 * @author hzc
 * @date 2025/7/28 11:42
 */
@Configuration
public class EncryptResolver implements EncryptablePropertyResolver {

    private final StringEncryptor encryptor;

    public EncryptResolver(@Qualifier("primaryEncryptor") StringEncryptor encryptor) {
        this.encryptor = encryptor;
    }

    @Override
    public String resolvePropertyValue(String value) {
        if (value != null && value.startsWith("ENC(") && value.endsWith(")")) {
            String encryptedValue = value.substring(4, value.length() - 1);
            // 默认解密器
            return encryptor.decrypt(encryptedValue);
        }
        return value;
    }

}
